package com.jspgou.cms.dao.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.jspgou.common.hibernate3.Finder;
import com.jspgou.common.hibernate3.HibernateBaseDao;
import com.jspgou.common.page.Pagination;
import com.jspgou.cms.dao.OrderDao;
import com.jspgou.cms.entity.Discuss;
import com.jspgou.cms.entity.Order;
import com.jspgou.cms.entity.ShopPaymentorder;

@Repository
public class OrderDaoImpl extends HibernateBaseDao<Order, Long> implements
		OrderDao {
	public Pagination getPage(int pageNo, int pageSize) {
		Criteria crit = createCriteria();
		Pagination page = findByCriteria(crit, pageNo, pageSize);
		return page;
	}
	
//	public Pagination getPageForMember(Long memberId, int pageNo, int pageSize) {//会员中心(我的订单)
//		Finder f = Finder.create("from Order bean where bean.member.id=:memberId");
//		f.setParam("memberId", memberId);
//		f.append(" and bean.status=40");
//		f.append(" order by bean.id desc");
//		return find(f, pageNo, pageSize);
//	}
//	public Pagination getPageForMember1(Long memberId, int pageNo, int pageSize) {//会员中心(我的待付款订单)
//		Finder f = Finder.create("from Order bean where bean.member.id=:memberId");
//		f.setParam("memberId", memberId);
//		f.append(" and bean.status>=10 and bean.status<=19");
//		f.append(" order by bean.id desc");
//		return find(f, pageNo, pageSize);
//	}
//	public Pagination getPageForMember2(Long memberId, int pageNo, int pageSize) {//会员中心(我的处理中订单)
//		Finder f = Finder.create("from Order bean where bean.member.id=:memberId");
//		f.setParam("memberId", memberId);
//		f.append(" and bean.status>=20 and bean.status<=29");
//		f.append(" order by bean.id desc");
//		return find(f, pageNo, pageSize);
//	}
//	public Pagination getPageForMember3(Long memberId, int pageNo, int pageSize) {//会员中心(全部订单)
//		Finder f = Finder.create("from Order bean where bean.member.id=:memberId");
//		f.setParam("memberId", memberId);
//		f.append(" order by bean.id desc");
//		return find(f, pageNo, pageSize);
//	}
	
	//店铺中心（订单列表）
	public Pagination getPage(Long webId,Long memberId,Long storeId, Integer status,Boolean isRefunded,String code,Long source,
			String productName, String userName,Long paymentId, Long shippingId,Integer paymentStatus,
			Integer shippingStatus,Long discuss,Date startTime,Date endTime,Double startOrderTotal,Double endOrderTotal,
			int pageNo, int pageSize) {
		Finder f = Finder.create("from Order bean where 1=1 ");	
		if(webId!=null){
			f.append(" and bean.website.id=:webId");
			f.setParam("webId", webId);
		}
		if(memberId!=null){
			f.append(" and bean.member.id=:memberId");
			f.setParam("memberId", memberId);
		}
		if(storeId!=null){
			f.append(" and bean.store.id=:storeId");
			f.setParam("storeId", storeId);
		}
		if (!StringUtils.isBlank(userName)) {
			f.append(" and bean.receiveName like:userName");
			f.setParam("userName", "%"+userName+"%");
		}
		if (!StringUtils.isBlank(productName)) {
			f.append(" and bean.productName like:productName");
			f.setParam("productName", "%"+productName+"%");
		}
		//判断商品来源
		if(source!=null){
			if(source==0){
				f.append(" and bean.store is null");
			}else if(source==1){
				f.append(" and bean.store is not null");
			}
		}
		if(paymentId!=null){
			f.append(" and bean.payment.id=:paymentId");
			f.setParam("paymentId", paymentId);
		}
		if(shippingId!=null){
			f.append(" and bean.shipping.id=:shippingId");
			f.setParam("shippingId", shippingId);
		}
		if(!StringUtils.isBlank(code)){
			f.append(" and bean.code=:code");
			f.setParam("code", code);
		}
		if(paymentStatus!=null){
			f.append(" and bean.paymentStatus=:paymentStatus");
			f.setParam("paymentStatus", paymentStatus);
		}
		if(shippingStatus!=null){
			f.append(" and bean.shippingStatus=:shippingStatus");
			f.setParam("shippingStatus", shippingStatus);
		}
		if(discuss!=null){
			Session session = getSession();		
			String hql = "select bean.orderItem.itemOrder.id from Discuss bean";    
			Query query = session.createQuery(hql);  
			if(query.list().size()>0){	
				f.append(" and bean.id not in(:discuss)");
				f.setParamList("discuss", query.list());
			}
		}
		if(startTime!=null){
			f.append(" and bean.createTime>:startTime");
			f.setParam("startTime", startTime);
		}
		if(endTime!=null){
			f.append(" and bean.createTime<:endTime");
			f.setParam("endTime", endTime);
		}
		if(startOrderTotal!=null){
			f.append(" and bean.total>=:startOrderTotal");
			f.setParam("startOrderTotal", startOrderTotal);
		}
		if(endOrderTotal!=null){
			f.append(" and bean.total<=:endOrderTotal");
			f.setParam("endOrderTotal", endOrderTotal);
		}
		if (status!=null) {
			f.append(" and bean.status=:status");
			f.setParam("status", status);
		}
		if (isRefunded!=null){
			f.append(" and bean.refunded=:refunded");
			f.setParam("refunded", isRefunded);
		}
		f.append(" order by bean.id desc");
		return find(f, pageNo, pageSize);
	}
	
	@SuppressWarnings("unchecked")
	public List<Order> getList(Long webId,Long memberId,Long storeId, Integer status,Integer paymentStatus,
			String productName,String userName,Long paymentId,Long shippingId, 
			Date startTime,Date endTime,Double startOrderTotal,Double endOrderTotal){
		Finder f = Finder.create("from Order bean where 1=1 ");	
		if(webId!=null){
			f.append(" and bean.website.id=:webId");
			f.setParam("webId", webId);
		}
		if(memberId!=null){
			f.append(" and bean.member.id=:memberId");
			f.setParam("memberId", memberId);
		}
		if(storeId!=null){
			f.append(" and bean.store.id=:storeId");
			f.setParam("storeId", storeId);
		}
		if (!StringUtils.isBlank(userName)) {
			f.append(" and bean.shopMemberAddress.username like:userName");
			f.setParam("userName", "%"+userName+"%");
		}
		if (!StringUtils.isBlank(productName)) {
			f.append(" and bean.productName like:productName");
			f.setParam("productName", "%"+productName+"%");
		}
		if(paymentId!=null){
			f.append(" and bean.payment.id=:paymentId");
			f.setParam("paymentId", paymentId);
		}
		if(shippingId!=null){
			f.append(" and bean.shipping.id=:shippingId");
			f.setParam("shippingId", shippingId);
		}
		if(startTime!=null){
			f.append(" and bean.createTime>:startTime");
			f.setParam("startTime", startTime);
		}
		if(endTime!=null){
			f.append(" and bean.createTime<:endTime");
			f.setParam("endTime", endTime);
		}
		if(startOrderTotal!=null){
			f.append(" and bean.total>=:startOrderTotal");
			f.setParam("startOrderTotal", startOrderTotal);
		}
		if(endOrderTotal!=null){
			f.append(" and bean.total<=:endOrderTotal");
			f.setParam("endOrderTotal", endOrderTotal);
		}
		if (status !=null) {
			f.append(" and bean.status=:status");
			f.setParam("status", status);
		}
		if(paymentStatus!=null){
			f.append(" and bean.paymentStatus=:paymentStatus");
			f.setParam("paymentStatus", paymentStatus);
		}
		f.append(" order by bean.createTime desc");
		return find(f);
	}
	
	//添加方法，根据订单状态获和订单时间取所有订单
    public List<Order> getCountByStatus(Date startTime,Date endTime,Integer status){
    	Finder finder=Finder.create("SELECT DATE_FORMAT(createTime, '%Y-%m-%d' ), COUNT(*) from Order bean where 1=1");
    
    	if(startTime!=null){
    		finder.append(" and bean.createTime>:startTime");
    		finder.setParam("startTime", startTime);
    	}
    	if(endTime!=null){
    		finder.append(" and bean.createTime<=:endTime");
    		finder.setParam("endTime", endTime);
    		
    	}
    	if(status!=null){
    		finder.append(" and bean.status=:status");
    		finder.setParam("status",status);
    		
    	}
    	finder.append(" GROUP BY DATE_FORMAT( createTime, '%Y-%m-%d' )");
    	
    	return find(finder);
    }
    
    //根据总订单，查出副订单
    @SuppressWarnings("unchecked")
    public List<Order> getPaymentOrder(ShopPaymentorder shopPaymentorder){
    	Finder f=Finder.create("from Order bean where bean.shopPaymentorder=:shopPaymentorder").setParam("shopPaymentorder", shopPaymentorder);
		List<Order> list=find(f);
		return list;
    }
    
    //根据订单状态，获取年度统计
    
    public List<Order> getStatisticByYear(Integer year,Integer status){
    	Finder finder=Finder.create("SELECT bean.createTime,COUNT(*) from Order bean where 1=1");
    	 if(status!=null){
    		finder.append(" and bean.status=:status");
    		finder.setParam("status",status);    		 
    	 }
    	 if(year!=null){
    		 String y = year.toString();
    		 finder.append(" and DATE_FORMAT(bean.createTime,'%Y') =:year");
    		 finder.setParam("year", y);
    	 }
    	 finder.append(" GROUP BY DATE_FORMAT(bean.createTime,'%Y%m')");
    	return find(finder);
    }

	public List<Object> getTotalOrder(){//后台首页统计函数
		List<Object> o=new ArrayList<Object>();
		//总订单数
		Long totalOrder=(Long)this.getSession().createQuery("select count(*) from Order bean").uniqueResult();
		//未完成订单数
		Long noCompleteOrder=(Long)this.getSession().
		createQuery("select count(*) from Order bean where bean.status in(1,2)").uniqueResult();
		
	    Calendar c=Calendar.getInstance();
	    String month=(c.get(Calendar.MONTH)+1)+"";
	    String year=c.get(Calendar.YEAR)+"";
	    if(month.length()==1){
	    	month="0"+month;
	    }else{
	    	month=month;
	    }
	    String str=year+"-"+month;
	    //本月订单数
		Long thisMontyOrder=(Long)this.getSession().
	    createQuery("select count(*) from Order bean where bean.createTime like :time")
	    .setString("time", "%"+str+"%").uniqueResult();
		
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		String tady=sf.format(new Date());
		//今日订单数
		Long todayOrder=(Long)this.getSession()
		.createQuery("select count(*) from Order bean where bean.createTime like :tody")
		.setString("tody", "%"+tady+"%").uniqueResult();
		//总产品数
		Long totalProduct=(Long)this.getSession().createQuery("select count(*) from Product bean")
		//Long totalProduct=(Long)this.getSession().createQuery("select sum(bean.stockCount) from ProductFashion bean")
		.uniqueResult();
		//本月产品数
	    Long newProductMonth=(Long)this.getSession().
	    createQuery("select count(*) from Product bean where bean.createTime like :time")
	    .setString("time", "%"+str+"%").uniqueResult();
	    //今日总产品数
	    Long dateProduct=(Long)this.getSession().
	    createQuery("select count(*) from Product bean where bean.createTime like :time")
	    .setString("time", "%"+tady+"%").uniqueResult();
	    //总会员数
	    Long totalMember=(Long)this.getSession().
	    createQuery("select count(*) from ShopMember bean").uniqueResult();
	    //本月会员数
	    Long totalMonthMember=(Long)this.getSession().
	    createQuery("select count(*) from Member bean where bean.createTime like :time")
	    .setString("time", "%"+str+"%").uniqueResult();
	    //今日会员数
	    Long totalDateMember=(Long)this.getSession().
	    createQuery("select count(*) from Member bean where bean.createTime like :time")
	    .setString("time", "%"+tady+"%").uniqueResult();
	    
	   c.add(Calendar.DATE, -7);
	   Date oldDate= c.getTime();
	   //最近七天会员数
	   Long date7Member=(Long)this.getSession().
	   createQuery("select count(*) from Member bean where bean.createTime >:time")
	   .setParameter("time", oldDate).uniqueResult();
	   
	   @SuppressWarnings("unused")
	   BigDecimal totalProfit=(BigDecimal)this.getSession().
	   createQuery("select sum((bean.finalPrice-bean.costPrice)*bean.count) from OrderItem bean ")
	   .uniqueResult();//总利润
	   //今日入账
	   BigDecimal dateMoneyruzhang=(BigDecimal)this.getSession().
	   createQuery("select sum((bean.finalPrice)* bean.count) from OrderItem bean where bean.itemOrder.status=4 and bean.itemOrder.paymentStatus=2 and bean.itemOrder.createTime like :time")
	   .setString("time","%"+tady+"%").uniqueResult();
	   //今日消费
	   BigDecimal dateMoneyxiaofei=(BigDecimal)this.getSession().
	   createQuery("select sum((bean.finalPrice)* bean.count) from OrderItem bean where bean.itemOrder.createTime like :time")
	   .setString("time","%"+tady+"%").uniqueResult();
	   //本月入账
	   BigDecimal monthMoneyruzhang=(BigDecimal)this.getSession().
	   createQuery("select sum((bean.finalPrice)* bean.count) from OrderItem bean where bean.itemOrder.status=4 and bean.itemOrder.paymentStatus=2 and bean.itemOrder.createTime like :time")
	   .setString("time", "%"+str+"%").uniqueResult();
	   //本月消费
	   BigDecimal monthMoneyxiaofei=(BigDecimal)this.getSession().
	   createQuery("select sum((bean.finalPrice)* bean.count) from OrderItem bean where bean.itemOrder.createTime like :time")
	   .setString("time", "%"+str+"%").uniqueResult();
	   
	    Long[] cc=new Long[11];
	    cc[0]=totalOrder==null?0:totalOrder;
	    cc[1]=noCompleteOrder==null?0:noCompleteOrder;
	    cc[2]=thisMontyOrder==null?0:thisMontyOrder;
	    cc[3]=todayOrder==null?0:todayOrder;
	    cc[4]=totalProduct==null?0:totalProduct;
	    cc[5]=newProductMonth==null?0:newProductMonth;
	    cc[6]=dateProduct==null?0:dateProduct;
	    cc[7]=totalMember==null?0:totalMember;
	    cc[8]=totalMonthMember==null?0:totalMonthMember;
	    cc[9]=totalDateMember==null?0:totalDateMember;
	    cc[10]=date7Member==null?0:date7Member;
	    BigDecimal [] bb=new BigDecimal[4];
	    bb[0]=(dateMoneyruzhang==null?new BigDecimal(0):dateMoneyruzhang);
	    bb[1]=(dateMoneyxiaofei==null?new BigDecimal(0):dateMoneyxiaofei);
	    bb[2]=(monthMoneyruzhang==null?new BigDecimal(0):monthMoneyruzhang);
	    bb[3]=(monthMoneyxiaofei==null?new BigDecimal(0):monthMoneyxiaofei);
	    o.add(cc);
	    o.add(bb);
	    return o;
	}
    
	//前台店铺首页统计
	public List<Object> getStoreTotalOrder(Long StoreId){
		List<Object> o=new ArrayList<Object>();
		//总订单数
		Long totalOrder=(Long)this.getSession().createQuery("select count(*) from Order bean where bean.store.id=:storeId")
		.setLong("storeId", StoreId).uniqueResult();
		//未完成订单数
		Long noCompleteOrder=(Long)this.getSession().
		createQuery("select count(*) from Order bean where bean.status in(1,2) and bean.store.id=:storeId")
		.setLong("storeId", StoreId).uniqueResult();
		
	    Calendar c=Calendar.getInstance();
	    String month=(c.get(Calendar.MONTH)+1)+"";
	    String year=c.get(Calendar.YEAR)+"";
	    if(month.length()==1){
	    	month="0"+month;
	    }else{
	    	month=month;
	    }
	    String str=year+"-"+month;
	    //本月订单数
		Long thisMontyOrder=(Long)this.getSession().
	    createQuery("select count(*) from Order bean where bean.createTime like :time and bean.store.id=:storeId")
	    .setString("time", "%"+str+"%").setLong("storeId", StoreId).uniqueResult();
		
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		String tady=sf.format(new Date());
		//今日订单数
		Long todayOrder=(Long)this.getSession()
		.createQuery("select count(*) from Order bean where bean.createTime like :tody and bean.store.id=:storeId")
		.setString("tody", "%"+tady+"%").setLong("storeId", StoreId).uniqueResult();
		//总产品数
		Long totalProduct=(Long)this.getSession().createQuery("select count(*) from Product bean where bean.store.id=:storeId")
		.setLong("storeId", StoreId).uniqueResult();
		//本月产品数
	    Long newProductMonth=(Long)this.getSession().
	    createQuery("select count(*) from Product bean where bean.createTime like :time and bean.store.id=:storeId")
	    .setString("time", "%"+str+"%").setLong("storeId", StoreId).uniqueResult();
	    //今日总产品数
	    Long dateProduct=(Long)this.getSession().
	    createQuery("select count(*) from Product bean where bean.createTime like :time and bean.store.id=:storeId")
	    .setString("time", "%"+tady+"%").setLong("storeId", StoreId).uniqueResult();
	    
	   c.add(Calendar.DATE, -7);
	   Date oldDate= c.getTime();
	   //最近七天产品数
	   Long date7Product=(Long)this.getSession().
	   createQuery("select count(*) from Product bean where bean.createTime >:time and bean.store.id=:storeId")
	   .setParameter("time", oldDate).setLong("storeId", StoreId).uniqueResult();
	   
	   @SuppressWarnings("unused")
	   //总利润
	   BigDecimal totalProfit=(BigDecimal)this.getSession().
	   createQuery("select sum((bean.finalPrice-bean.costPrice)*bean.count) from OrderItem bean where bean.itemOrder.store.id=:storeId")
	   .setLong("storeId", StoreId).uniqueResult();
	   //今日入账
	   BigDecimal dateMoneyruzhang=(BigDecimal)this.getSession().
	   createQuery("select sum((bean.finalPrice)* bean.count) from OrderItem bean where bean.itemOrder.status=4 and bean.itemOrder.paymentStatus=2 and bean.itemOrder.createTime like :time and bean.itemOrder.store.id=:storeId")
	   .setString("time","%"+tady+"%").setLong("storeId", StoreId).uniqueResult();
	   //今日消费
	   BigDecimal dateMoneyxiaofei=(BigDecimal)this.getSession().
	   createQuery("select sum((bean.finalPrice)* bean.count) from OrderItem bean where bean.itemOrder.createTime like :time and bean.itemOrder.store.id=:storeId")
	   .setString("time","%"+tady+"%").setLong("storeId", StoreId).uniqueResult();
	   //本月入账
	   BigDecimal monthMoneyruzhang=(BigDecimal)this.getSession().
	   createQuery("select sum((bean.finalPrice)* bean.count) from OrderItem bean where bean.itemOrder.status=4 and bean.itemOrder.paymentStatus=2 and bean.itemOrder.createTime like :time and bean.itemOrder.store.id=:storeId")
	   .setString("time", "%"+str+"%").setLong("storeId", StoreId).uniqueResult();
	   //本月消费
	   BigDecimal monthMoneyxiaofei=(BigDecimal)this.getSession().
	   createQuery("select sum((bean.finalPrice)* bean.count) from OrderItem bean where bean.itemOrder.createTime like :time and bean.itemOrder.store.id=:storeId")
	   .setString("time", "%"+str+"%").setLong("storeId", StoreId).uniqueResult();
	   
	    Long[] cc=new Long[8];
	    cc[0]=totalOrder==null?0:totalOrder;
	    cc[1]=noCompleteOrder==null?0:noCompleteOrder;
	    cc[2]=thisMontyOrder==null?0:thisMontyOrder;
	    cc[3]=todayOrder==null?0:todayOrder;
	    cc[4]=totalProduct==null?0:totalProduct;
	    cc[5]=newProductMonth==null?0:newProductMonth;
	    cc[6]=dateProduct==null?0:dateProduct;
	    cc[7]=date7Product==null?0:date7Product;
	    BigDecimal [] bb=new BigDecimal[5];
	    bb[0]=(dateMoneyruzhang==null?new BigDecimal(0):dateMoneyruzhang);
	    bb[1]=(dateMoneyxiaofei==null?new BigDecimal(0):dateMoneyxiaofei);
	    bb[2]=(monthMoneyruzhang==null?new BigDecimal(0):monthMoneyruzhang);
	    bb[3]=(monthMoneyxiaofei==null?new BigDecimal(0):monthMoneyxiaofei);
	    bb[4]=(totalProfit==null?new BigDecimal(0):totalProfit);
	    o.add(cc);
	    o.add(bb);
	    return o;
	}
	
	public BigDecimal getMemberMoneyByYear(Long memberId){//会员今年的消费
		Calendar c=Calendar.getInstance();
		String year=""+c.get(Calendar.YEAR);
		Query q=this.getSession().createQuery("select sum((bean.finalPrice)* bean.count) from OrderItem bean" +
				" where bean.itemOrder.member.id=:id and bean.itemOrder.status=4 and bean.itemOrder.paymentStatus=2 and bean.itemOrder.createTime like:time");
		q.setParameter("id", memberId).setString("time", "%"+year+"%");
		return (BigDecimal)q.uniqueResult();
	}
	
	public Integer[] getOrderByMember(Long memberId){//会员我的订单（完成，未完成）
		Long succOrder=(Long)this.getSession().createQuery("select count(*) from Order bean where bean.status =40 and bean.member.id=:id")
		.setParameter("id", memberId).uniqueResult();
		Long failOrder=(Long)this.getSession().createQuery("select count(*) from Order bean where bean.status =41 and bean.member.id=:id")
		.setParameter("id", memberId).uniqueResult();
		
		Long totalOrder=(Long)this.getSession().createQuery("select count(*) from Order bean where bean.member.id=:id")//全部订单
		.setParameter("id", memberId).uniqueResult();
		
		Long pendIngOrder=(Long)this.getSession().createQuery("select count(*) from Order bean where bean.member.id=:id and bean.status>=10 and bean.status<=19")//待付款订单
		.setParameter("id", memberId).uniqueResult();
		
		Long proceOrder=(Long)this.getSession().createQuery("select count(*) from Order bean where bean.member.id=:id and bean.status>=20 and bean.status<=29")//待处理订单
		.setParameter("id", memberId).uniqueResult();
		
		Integer[] orders = new Integer[5];
		orders[0] = succOrder.intValue();
		orders[1] = failOrder.intValue();//退回订单
		orders[2] = totalOrder.intValue();
		orders[3] = pendIngOrder.intValue();
		orders[4] = proceOrder.intValue();
		return orders;
	}
	
	public Pagination getOrderByReturn(Long memberId,int pageNo,int pageSize){//会员中心，退货订单
		Finder f = Finder.create("from Order bean where bean.returnOrder.id is not null");
		if(memberId!=null){
			f.append(" and bean.member.id=:memberId").setParam("memberId", memberId);
		}
		f.append(" order by bean.id desc");
		return find(f, pageNo, pageSize);
	}
	
	public Pagination getFcPage(Integer paymentStatus,Integer shippingStatus,Integer status,Long storeId,Long memberId,int pageNo, int pageSize){
		Finder f=Finder.create("from Order bean where bean.returnOrder.id is not null");
		if(paymentStatus!=null){
			f.append(" and bean.paymentStatus=:paymentStatus");
			f.setParam("paymentStatus", paymentStatus);
		}
		if(shippingStatus!=null){
			f.append(" and bean.shippingStatus=:shippingStatus");
			f.setParam("shippingStatus", shippingStatus);
		}
		if(status!=null){
			f.append(" and bean.returnOrder.status=:status").setParam("status", status);
		}
		if(memberId!=null){
			f.append(" and bean.member.id=:memberId").setParam("memberId", memberId);
		}
		if(storeId!=null){
			f.append(" and bean.store.id=:storeId");
			f.setParam("storeId", storeId);
		}
		f.append(" order by bean.returnOrder.createTime desc");
		return find(f,pageNo,pageSize);
	}
	
	public Order findById(Long id) {
		Order entity = get(id);
		return entity;
	}
	
	@SuppressWarnings("unchecked")
	public Order findByCode(String code){
		Finder f=Finder.create("from Order bean where bean.code=:code").setParam("code", code);
		List<Order> list=find(f);
		if(list.size()==1){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public Order findByTradeNo(String tradeNo){
		Finder f = Finder.create("from Order bean where bean.tradeNo=:tradeNo").setParam("tradeNo", tradeNo);
		List<Order> list=find(f);
		if(list.size()==1){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	public List<Order> getList1(Long[]memberId){
 		Finder f=Finder.create(" from Order bean where 1=1");
 		if(memberId!=null){
 		f.append(" and bean.member.id=:memberId");
		f.setParam("memberId", memberId);
 			
 		}
 		return find(f);
	}
	
	public List<Order> getlist(Date endDate){
		Finder f=Finder.create("from Order bean where 1=1");
		f.append(" and bean.paymentStatus=:paymentStatus");
		f.append(" and bean.createTime<:endTime");
		f.setParam("paymentStatus", PAYMENTSTATUS);
		f.setParam("endTime", endDate);
		return find(f);
	}
	


	public Order save(Order bean) {
		getSession().save(bean);
		return bean;
	}

	public Order deleteById(Long id) {
		Order entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}

	@Override
	protected Class<Order> getEntityClass() {
		return Order.class;
	}
	
	/**
	 * 等待支付
	 */
	public static final Integer PAYMENTSTATUS=1;
	
}